package com.clz.workorder.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.clz.workorder.R
import com.clz.workorder.databinding.ItemAttachImageBinding
import com.clz.workorder.databinding.ItemOrderStateBinding
import com.clz.workorder.fragment.OrderDetailFragment
import com.clz.workorder.view.FullyGridLayoutManager
import com.czl.base.data.bean.ReportOrderState

class OrderStateAdapter(val fragment: Fragment):RecyclerView.Adapter<BaseDataBindingHolder<ItemOrderStateBinding>>() {
    var hasLastFinish=false
    var sl:ArrayList<ReportOrderState> = ArrayList()
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseDataBindingHolder<ItemOrderStateBinding> {
        var view= LayoutInflater.from(parent.context).inflate(R.layout.item_order_state,null)
        return BaseDataBindingHolder<ItemOrderStateBinding>(view)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(
        holder: BaseDataBindingHolder<ItemOrderStateBinding>,
        position: Int
    ) {
        holder.dataBinding!!.item=sl[position]
        holder.dataBinding!!.adapter=this
        holder.dataBinding!!.position=position
        hasLastFinished()
        holder.dataBinding!!.lineshow=getPositionShow(position)
        if (sl[position].imgs!=null&& sl[position].imgs!!.size>0){
            holder.dataBinding!!.rvPsAttach.apply {
//                visibility=View.VISIBLE
                layoutManager= FullyGridLayoutManager(context,4, GridLayoutManager.VERTICAL, false)
                adapter=OrderStateAttachImageAdapter(fragment).apply {
                    urls= sl[position].imgs!!
                    notifyDataSetChanged()
                }
            }
        }else{
            holder.dataBinding!!.rvPsAttach.apply {
//                visibility=View.GONE
                if (adapter!=null){
                    adapter=null
                }
            }
        }
        holder.dataBinding!!.executePendingBindings()
    }
    fun hasLastFinished(){
        hasLastFinish = sl[sl.size-1].state=="已评价"
    }

    override fun getItemCount(): Int {
        return sl.size ?: 0
    }
    fun hasPingjia(state:String):Boolean{
        return state=="已评价"
    }
    fun getPositionShow(position:Int):LineData{
        var lv=true
        var lc=true
        var ov=true
        var oc=true
        var olv=true
        var olc=true
        if (sl.size>1){
            if (position==0){
                olv=false
            }
            if (position==sl.size-1){
                lv=false
                if (!hasLastFinish){
                    olc=false
                }
            }
            if (position==sl.size-2){
                if (!hasLastFinish){
                    lc=false
                }
            }
        }else{
            lv=false
            olv=false
            if (!hasLastFinish){
                oc=false
            }
        }
        return LineData(lv,lc,ov,oc,olv,olc)
    }
    data class LineData(val lineViSible:Boolean,val lineChecked:Boolean,
    val ovalVisible:Boolean,val ovalChecked:Boolean,
    val ovallineVisible:Boolean,val ovalLineChecked:Boolean)

}